package com.gitee.neuray.security.admin.executor.handler;

import com.gitee.neuray.security.common.msg.ObjectRestResponse;
import com.gitee.neuray.security.common.util.CommonUtil;
import com.gitee.neuray.security.mail.feign.MailServiceFeignClient;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @auther: zhuqz
 * @date: 2020/3/2 15:19
 * @description:
 */
@Component
@Slf4j
public class MailXxJob {
    @Autowired
    MailServiceFeignClient client;
    /**
     * 定时任务发送邮件
     */
    @XxlJob("sendMailHandler")
    public ReturnT<String> sendMailHandler(String param) throws Exception {
        SimpleDateFormat simpleDateFormat =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>邮件执行开始......"+simpleDateFormat.format(new Date()));
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>邮件执行开始......"+simpleDateFormat.format(new Date()));
        ObjectRestResponse res = null;
        try {
            res = client.sendTimerMail();
        } catch (Exception e) {
            XxlJobLogger.log("=================发送定时邮件报错");
            XxlJobLogger.log(CommonUtil.getExceptionDtail(e));
            log.error(CommonUtil.getExceptionDtail(e));
            throw e;
        }
        if(res!=null && res.isRel()){
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>邮件执行成功......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>邮件执行成功......");
            return ReturnT.SUCCESS;
        }else {
            XxlJobLogger.log(">>>>>>>>>>>>>>>>>>>>>>>>邮件执行失败......");
            log.info(">>>>>>>>>>>>>>>>>>>>>>>>邮件执行失败......");
            return ReturnT.FAIL;
        }

    }
}